package shuashuami.hb.com.avtivity.agent;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import shuashuami.hb.com.avtivity.AbActivity;
import shuashuami.hb.com.fragment.agent.ClickMangeFragm;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.view.PagerSlidingTabStrip;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/23<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/23<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class ClickerManagerAct extends AbActivity {

    private ViewPager contentPager;
    private mPagerAdapter adapter;
    private PagerSlidingTabStrip tabs;

    private LinearLayout lin_click_menu;
    private LinearLayout lin_click_menu2;

    ClickMangeFragm mAllClickFragm,mAuditClickFragm;

    private Animation myAnimation_Translate;

    private boolean menuIsOpen=false;

    private int currentPage=0;

    private ImageView btn_left;

    private EditText edit_next;

    private ImageView image_searchbtn;
    private boolean isSearch=false;
    @Override
    protected void initUI() {
        setAbContentView(R.layout.act_aclickmaage);
        lin_click_menu= (LinearLayout) findViewById(R.id.lin_click_menu);
        lin_click_menu2= (LinearLayout) findViewById(R.id.lin_click_menu2);
        contentPager = (ViewPager) findViewById(R.id.content_pager_aclick);
        adapter = new mPagerAdapter(getSupportFragmentManager());
        contentPager.setAdapter(adapter);
        contentPager.setOffscreenPageLimit(2);
        edit_next= (EditText) findViewById(R.id.edit_next);
        image_searchbtn= (ImageView) findViewById(R.id.image_searchbtn);

        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs_aclick_center);
        tabs.setTextColorResource(R.color.gray);
        tabs.setDividerColorResource(R.color.view_unpressed);
        tabs.setIndicatorColorResource(R.color.blue);
        tabs.setSelectedTextColorResource(R.color.blue);
        tabs.setIndicatorHeight(4);
        tabs.setUnderlineHeight(2);
        tabs.setTextSize(12);
        tabs.setViewPager(contentPager);

        tabs.setPullView(0);
    }
//
    public void onMenuClick(View v){
        int tag=Integer.parseInt(v.getTag().toString()) ;
        if(tag==5){
            closeMenu();
            return;
        }
        // 取消所有的特效
        for(int i=0;i<lin_click_menu2.getChildCount();i+=2){
            LinearLayout child = (LinearLayout) lin_click_menu2.getChildAt(i);
            TextView text = (TextView) child.getChildAt(0);
            ImageView imga = (ImageView) child.getChildAt(1);
            if(i==tag*2){
                text.setTextColor(getResources().getColor(R.color.app_main));
                imga.setVisibility(View.VISIBLE);
            }else {
                text.setTextColor(Color.BLACK);
                imga.setVisibility(View.GONE);
            }
        }

        mAllClickFragm.setCurrentLevel(tag);
        mAuditClickFragm.setCurrentLevel(tag);
        closeMenu();

    }

    @Override
    protected void initData() {
        setLeftView();
        Bundle bundle=new Bundle();
        bundle.putInt("type",1);
        mAllClickFragm = new ClickMangeFragm();
        mAllClickFragm.setArguments(bundle);

        Bundle bundle1=new Bundle();
        bundle1.putInt("type",2);
        mAuditClickFragm=new ClickMangeFragm();
        mAuditClickFragm.setArguments(bundle1);
    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tabs.registerOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tag=Integer.parseInt(v.getTag().toString());
                if(tag==0 && currentPage==tag) {
                    if(!menuIsOpen) {
                        openMenu();
                    }else{
                        closeMenu();
                    }
                }else{
                    closeMenu();
                }
                currentPage=tag;
            }
        });

        edit_next.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            //            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    edit_next.bringToFront();
                    isSearch=false;
                }else{
                    image_searchbtn.bringToFront();
                    isSearch=true;
                }

            }
        });

        image_searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSearch){
                    edit_next.clearFocus();

                    mAllClickFragm.disPlayData(edit_next.getText().toString());
                    mAuditClickFragm.disPlayData(edit_next.getText().toString());
                }
            }
        });
    }


    public void openMenu(){
        menuIsOpen=true;
        lin_click_menu.setVisibility(View.VISIBLE);
        tabs.upPullView(0);

        myAnimation_Translate = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF, -1,
                Animation.RELATIVE_TO_SELF, 0);
        myAnimation_Translate.setDuration(200);
        myAnimation_Translate.setInterpolator(AnimationUtils
                .loadInterpolator(ClickerManagerAct.this,
                        android.R.anim.accelerate_decelerate_interpolator));
        lin_click_menu2.startAnimation(myAnimation_Translate);
    }


    public void closeMenu(){
        tabs.downPullView(0);
        menuIsOpen=false;
        lin_click_menu.setVisibility(View.GONE);
    }

    Fragment fragment;

    private class mPagerAdapter extends FragmentStatePagerAdapter {

        private String Title[] = {"全部刷手", "待审核"};

        public mPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            // int[] id = { 5, 18, 27, 37, 21, 36, 23, 24 };
            if (arg0 == 0) {
//                if(fragment==null){
//                    fragment=new TestFragment();
//                }
//                return fragment;
                return mAllClickFragm;
            } else {

                return mAuditClickFragm;
            }
        }

        @Override
        public int getCount() {
            return Title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Title[position];
        }

    }
}
